﻿using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace NotificationTester
{
	static class Program
	{
		static void Main(string[] args)
		{
			Console.Out.WriteLine("Notification service connection test");
			Console.Out.WriteLine("(c) Henning Krause 2009");
			Console.Out.WriteLine();
			if (args == null || args.Length == 0)
			{
				Console.Out.WriteLine("No callback address specifed. Please specify the address of the service expecting notifications.");
				Console.Out.WriteLine();
				return;
			}

			Console.Out.WriteLine("Trying to connect to the notification listener on {0}...", args[0]);
			var url = new Uri(args[0]);
			var connector = new NotificationServiceConnector(url);

			try
			{
				var result = connector.TestConnection();
				switch (result)
				{
					case TestResult.Success:
						Console.Out.WriteLine("No error has been found.");
						break;
					case TestResult.ConnectFailed:
						Console.Out.WriteLine(
							"Failed to establish a connection to the specified host. To resolve this issue check the firewall settings on this machine and the host receiving the notifications.");
						break;
					case TestResult.ServerCertificateUntrusted:
						Console.Out.WriteLine(
							"The server certificate used by the notification service is not valid. Exchange will only send notifications to services using a valid and trusted certificate.");
						break;
					case TestResult.Timeout:
						Console.Out.WriteLine(
							"The notification service did not return the call within one minute. Maybe a firewall is blocking the connection.");
						break;
					case TestResult.DnsResolutionFailed:
						Console.Out.WriteLine(
							"DNS resolution of the host {0} failed. Exchange will not be able to send notifications to the specified address.", url.Host);
						break;
				}
			}
			catch (Exception ex)
			{
				Console.Out.WriteLine("An unexpected error occured: {0}\r\nException type: {1}", ex.Message, ex.GetType().FullName);
			}
		}
	}
}
